package com.suncaper.wcss.web.controller;

import com.suncaper.wcss.common.utls.SessionUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.suncaper.wcss.common.JsonResponse;
import com.suncaper.wcss.service.UserService;
import com.suncaper.wcss.model.domain.User;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2021-11-10
 * @version v1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger( UserController.class );

    @Autowired
    private UserService userService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        User  user =  userService.getById(id);
        return JsonResponse.success(user);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        userService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateUser(@PathVariable("id") Long  id,User  user) throws Exception {
        user.setId(id);
        userService.updateById(user);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建User
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(User  user) throws Exception {
        userService.save(user);
        return JsonResponse.success(null);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    @ResponseBody
    public JsonResponse login(User user){
       User loginUser = userService.login(user);
       if(Objects.nonNull(loginUser)){
           SessionUtils.saveCurrentUserInfo(loginUser);
       }
       return JsonResponse.success(loginUser);
    }

    /**
     * 等出
     */
    @GetMapping("/logout")
    public String logout(){
        SessionUtils.clear();
        return "login";
    }

    /**
     * 下载
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,User user){
        //1.不需要返回
        //2.参数要带有响应
        userService.export(response,user);
    }

    @PostMapping("/import")
    @ResponseBody
    public JsonResponse importUser(MultipartFile file){
       Map<String,Object> map = userService.importUser(file);
        return JsonResponse.success(map);
    }

    @GetMapping("/staByCollege")
    @ResponseBody
    public JsonResponse staByCollege(){
        return JsonResponse.success(userService.staByCollege());
    }
}

